package org.anupta.game.entity.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.anupta.game.entity.entities.Player;
import org.anupta.game.main.Main;
import org.anupta.game.utils.others.Config;
import org.anupta.game.utils.sql.Database;

public class PlayerDAO extends DAO<Player> {

	@Override
	public Player find(int id) {
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            Player player = null;
            try {
                connection = Database.getDatabase(Config.LOGIN[0]).getSource().getConnection();
                statement = connection.prepareStatement("SELECT * FROM players WHERE guid = ?;");
                statement.setInt(1, id);
                resultSet = statement.executeQuery();

                if (resultSet.first()) {
                    player = new Player(
                    		resultSet.getInt("guid"),
                    		resultSet.getString("name"),
                    		resultSet.getByte("gameGuid"),
                    		resultSet.getByte("sexe"),
                    		resultSet.getByte("classe"),
                    		resultSet.getInt("level"),
                    		resultSet.getInt("gfx"),
                    		resultSet.getInt("color1"),
                    		resultSet.getInt("color2"),
                    		resultSet.getInt("color3"),
                    		resultSet.getBoolean("new"),
                    		resultSet.getString("stuff"),
                    		resultSet.getInt("life"),
                    		resultSet.getString("channels")
                    		);
                }
                
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
            	Main.err.write(e);
            } finally {
                try {
                    resultSet.close();
                    statement.close();
                    connection.close();
                } catch (SQLException e) {
                	Main.err.write(e);
                }
            }
            return player;
	}

	@Override
	public Player find(String name) {
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            Player player = null;
            try {
                connection = Database.getDatabase(Config.LOGIN[0]).getSource().getConnection();
                statement = connection.prepareStatement("SELECT * FROM players WHERE name = ?;");
                statement.setString(1, name);
                resultSet = statement.executeQuery();

                if (resultSet.first()) {
                    player = new Player(
                    		resultSet.getInt("guid"),
                    		resultSet.getString("name"),
                    		resultSet.getByte("gameGuid"),
                    		resultSet.getByte("sexe"),
                    		resultSet.getByte("classe"),
                    		resultSet.getInt("level"),
                    		resultSet.getInt("gfx"),
                    		resultSet.getInt("color1"),
                    		resultSet.getInt("color2"),
                    		resultSet.getInt("color3"),
                    		resultSet.getBoolean("new"),
                    		resultSet.getString("stuff"),
                    		resultSet.getInt("life"),
                    		resultSet.getString("channels")
                    		);
                }
                
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
            	Main.err.write(e);
            } finally {
                try {
                    resultSet.close();
                    statement.close();
                    connection.close();
                } catch (SQLException e) {
                	Main.err.write(e);
                }
            }
            return player;
	}

	@Override
	public List<Player> findAll() {
		return null;
	}

	@Override
	public void commit() {
		synchronized(this){
			for (Player player : getCreate().values()) {
    			Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    			PreparedStatement statement = null;
    			try {
					statement = connection.prepareStatement("INSERT INTO players VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
					statement.setInt(1, player.getGuid());
					statement.setString(2, player.getName());
					statement.setInt(3, player.getGameId());
					statement.setInt(4, player.getSexe().getSexe());
					statement.setInt(5, player.getClasse().getId());
					statement.setInt(6, player.getLevel());
					statement.setInt(7, player.getGfx());
					statement.setInt(8, player.getColors()[0]);
					statement.setInt(9, player.getColors()[1]);
					statement.setInt(10, player.getColors()[2]);
					statement.setBoolean(11, player.isNew());
					if(player.getStuffString().isEmpty()){
						statement.setString(12, "");
					}else{
						statement.setString(12, player.getStuffString());
					}
					statement.setInt(13, player.getLife());
					statement.setString(14, player.getChannels());
					statement.execute();
					
					statement.close();
					connection.close();
					getCreate().remove(player.getGuid());
				} catch (SQLException e) {
					Main.err.write(e);
					e.printStackTrace();
				} finally {
					try {
						statement.close();
						connection.close();
						getCreate().remove(player.getGuid());
					} catch (SQLException e) {
						Main.err.write(e);
					}
				}
        	}

        	for (Player player : getUpdate().values()) {
        		Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    			PreparedStatement statement = null;
    			try {
    				statement = connection.prepareStatement("UPDATE players SET " +
    														"name = ?," +
    														"gameGuid = ?," +
    														"sexe = ?," +
    														"classe = ?," +
    														"level = ?," +
    														"gfx = ?," +
    														"color1 = ?," +
    														"color2 = ?," +
    														"color3 = ?," +
    														"new = ?," +
    														"stuff = ?," +
    														"life = ?," +
    														"channels = ?" +
    														" WHERE guid = ?;");
    				statement.setString(1, player.getName());
					statement.setInt(2, (byte) player.getGameId());
					statement.setInt(3, player.getSexe().getSexe());
					statement.setInt(4, player.getClasse().getId());
					statement.setInt(5, player.getLevel());
					statement.setInt(6, player.getGfx());
					statement.setInt(7, player.getColors()[0]);
					statement.setInt(8, player.getColors()[1]);
					statement.setInt(9, player.getColors()[2]);
					statement.setBoolean(10, player.isNew());
					statement.setString(11, player.getStuffString());
					statement.setInt(12, player.getLife());
					statement.setString(13, player.getChannels());
					statement.setInt(14, player.getGuid());
					statement.executeUpdate();
					
					statement.close();
					connection.close();
					getUpdate().remove(player.getGuid());
    			} catch (SQLException e) {
					Main.err.write(e);
				} finally {
					try {
						statement.close();
						connection.close();
						getUpdate().remove(player.getGuid());
					} catch (SQLException e) {
						Main.err.write(e);
					}
				}
        	}

        	for (Player player : getDelete().values()) {
        		Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    			PreparedStatement statement = null;
    			try {
					statement = connection.prepareStatement("DELETE FROM players WHERE guid = ?;");
					statement.setInt(1, player.getGuid());
					statement.executeUpdate();
					
					statement.close();
					connection.close();
					getDelete().remove(player.getGuid());
				} catch (SQLException e) {
					Main.err.write(e);
				} finally {
					try {
						statement.close();
						connection.close();
						getDelete().remove(player.getGuid());
					} catch (SQLException e) {
						Main.err.write(e);
					}
				}
        	}
		}
	}
	
	public static int getNewGuid(){
		Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int max = 0;
        try {
			connection = Database.getDatabase(Config.LOGIN[0]).getSource().getConnection();
			statement = connection.prepareStatement("SELECT MAX(guid) AS max FROM players;");
			resultSet = statement.executeQuery();
			if(resultSet.first()){
				max = (resultSet.getInt("max") + 1);
			}
			
			resultSet.close();
			statement.close();
			connection.close();
		} catch (SQLException e) {
			Main.err.write(e);
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				Main.err.write(e);
			}
		}
        return max;
	}
}
